﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AMS.Database;
using AMS.LINQ;

namespace AMS.Business
{
    public class AnalysisMethodClass
    {
        Int32 analysisMethodID;
        Int32 analysisMethodGroupID;
        String analysisMethodName;
        String analysisMethodNameFR;
        String analysisMethodNameEN;
        String analysisMethodNameDE;
        String analysisMethodVersion;
        bool active;

        bool isDirty = false;
        bool isNew = false;

        #region Properties
        public Int32 AnalysisMethodID
        {
            get
            {
                return analysisMethodID;
            }
        }

        public Int32 AnalysisMethodGroupID
        {
            get
            {
                return analysisMethodGroupID;
            }
            set
            {
                analysisMethodGroupID = value;
                isDirty = true;
            }
        }

        public String AnalysisMethodNameFR
        {
            get
            {
                return analysisMethodNameFR;
            }
            set
            {
                if (value == String.Empty)
                {
                    throw new Exception("Analysemethodenaam (frans) is niet optioneel");
                }
                analysisMethodNameFR = value;
                isDirty = true;
            }
        }

        public String AnalysisMethodNameDE
        {
            get
            {
                return analysisMethodNameDE;
            }
            set
            {
                if (value == String.Empty)
                {
                    throw new Exception("Analysemethodenaam (duits) is niet optioneel");
                }
                analysisMethodNameDE = value;
                isDirty = true;
            }
        }

        public String AnalysisMethodNameEN
        {
            get
            {
                return analysisMethodNameEN;
            }
            set
            {
                if (value == String.Empty)
                {
                    throw new Exception("Analysemethodenaam (engels) is niet optioneel");
                }
                analysisMethodNameEN = value;
                isDirty = true;
            }
        }

        public String AnalysisMethodName
        {
            get
            {
                return analysisMethodName;
            }
            set 
            {
                if (value == String.Empty)
                {
                    throw new Exception("Analysemethodenaam is niet optioneel");
                }
                analysisMethodName = value;
                isDirty = true;
            }
        }

        public String AnalysisMethodVersion
        {
            get
            {
                return analysisMethodVersion;
            }
            set
            {
                if (value == String.Empty)
                {
                    throw new Exception("Analysemethodeversie is niet optioneel");
                }
                analysisMethodVersion = value;
                isDirty = true;
            }
        }

        public bool Active
        {
            get
            {
                return active;
            }
            set
            {
                active = value;
                isDirty = true;
            }
        }
        #endregion

        #region Public methods
        public AnalysisMethodClass()
        {
            isNew = true;
            isDirty = false;
        }

        public void Save()
        {
            try
            {
                if (isNew)
                {
                    AddAnalysisMethod();
                }
                else if(isDirty)
                {
                    UpdateAnalysisMethod();
                }

                isNew = false;
                isDirty = false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Delete(Int32 ID)
        {
            try
            {
                #region Old code
                //DataAccess.BeginTransaction(out db, out conn, out trans);

                //SqlCommand comm = new SqlCommand();
                //comm.CommandType = CommandType.StoredProcedure;
                //comm.CommandText = "DeleteAnalysisMethod";

                //SqlParameter param = new SqlParameter();
                //param.ParameterName = "AnalysisMethodID";
                //param.Direction = ParameterDirection.Input;
                //param.Value = ID;
                //comm.Parameters.Add(param);

                //db.ExecuteNonQuery(comm,trans);

                //DataAccess.CommitTransaction(trans);
                #endregion

                var analysisToBeDeleted = from a in Common.AnalyseMethod().AnalysisMethods
                                          where a.AnalysisMethodID == analysisMethodID
                                          select a;

                Common.AnalyseMethod().AnalysisMethods.DeleteOnSubmit(analysisToBeDeleted.Single());
                Common.AnalyseMethod().SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Load(Int32 ID)
        {
            try
            {
                analysisMethodID = ID;

                #region Old code
                //DataAccess.BeginTransaction(out db, out conn, out trans);

                //SqlCommand comm = new SqlCommand();
                //comm.CommandType = CommandType.StoredProcedure;
                //comm.CommandText = "GetAnalysisMethod";

                //SqlParameter param = new SqlParameter();
                //param.ParameterName = "ID";
                //param.Direction = ParameterDirection.Input;
                //param.Value = analysisMethodID;
                //comm.Parameters.Add(param);

                //SqlDataReader reader = (SqlDataReader)db.ExecuteReader(comm);

                //// Expecting 1 record
                //while (reader.Read())
                //{
                //    analysisMethodGroupID = reader.GetInt32(0);
                //    analysisMethodName = reader.GetValue(1).ToString();
                //    analysisMethodNameFR = reader.GetValue(2).ToString();
                //    analysisMethodNameEN = reader.GetValue(3).ToString();
                //    analysisMethodNameDE = reader.GetValue(4).ToString();
                //    analysisMethodVersion = reader.GetValue(5).ToString();
                //    active = (bool)reader.GetValue(6);
                //}
                #endregion

                var am = from a in Common.AnalyseMethod().AnalysisMethods
                         where a.AnalysisMethodID == analysisMethodID
                         select a;

                var item = am.Single();

                analysisMethodGroupID = Convert.ToInt32(item.AnalysisMethodGroupID_FK);
                analysisMethodName = item.AnalysisMethodName;
                analysisMethodNameFR = item.AnalysisMethodNameFR;
                analysisMethodNameEN = item.AnalysisMethodNameEN;
                analysisMethodNameDE = item.AnalysisMethodNameDE;
                analysisMethodVersion = item.AnalysisMethodScientificVersion;
                active = item.Active;

                isNew = false;
                isDirty = false;

                //DataAccess.CommitTransaction(trans); 
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region Private methods
        private void AddAnalysisMethod()
        {
            try
            {
                #region Old code
                //comm.CommandType = CommandType.StoredProcedure;
                //comm.CommandText = "AddAnalysisMethod";
                //comm.Parameters.Clear();

                //SqlParameter param = new SqlParameter();
                //param.ParameterName = "@RetVal";
                //param.SqlDbType = SqlDbType.Int;
                //param.Direction = ParameterDirection.ReturnValue;
                //comm.Parameters.Add(param);

                //param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "Group";
                //param.Value = analysisMethodGroupID;
                //comm.Parameters.Add(param);

                //param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "Name";
                //param.Value = analysisMethodName;
                //comm.Parameters.Add(param);

                //param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "NameFR";
                //param.Value = analysisMethodNameFR;
                //comm.Parameters.Add(param);

                //param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "NameEN";
                //param.Value = analysisMethodNameEN;
                //comm.Parameters.Add(param);

                //param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "NameDE";
                //param.Value = analysisMethodNameDE;
                //comm.Parameters.Add(param);

                //param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "Version";
                //param.Value = analysisMethodVersion;
                //comm.Parameters.Add(param);

                //param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "Active";
                //param.Value = active;
                //comm.Parameters.Add(param);

                //db.ExecuteNonQuery(comm, trans);
                //analysisMethodID = Convert.ToInt32(comm.Parameters["@RetVal"].Value);

                //if (analysisMethodID == -1)
                //{
                //    throw new Exception("Er heeft zich een fout voorgedaan bij het toevoegen van een analysemethode.");
                //}
                #endregion

                AnalysisMethod newAnalysisMethod = new AnalysisMethod();
                newAnalysisMethod.Active = active;
                newAnalysisMethod.AnalysisMethodGroupID_FK = analysisMethodGroupID;
                newAnalysisMethod.AnalysisMethodName = analysisMethodName;
                newAnalysisMethod.AnalysisMethodNameDE = analysisMethodNameDE;
                newAnalysisMethod.AnalysisMethodNameEN = analysisMethodNameEN;
                newAnalysisMethod.AnalysisMethodNameFR = analysisMethodNameFR;
                newAnalysisMethod.AnalysisMethodScientificVersion = AnalysisMethodVersion;
                Common.AnalyseMethod().AnalysisMethods.InsertOnSubmit(newAnalysisMethod);
                Common.AnalyseMethod().SubmitChanges();

                analysisMethodID = newAnalysisMethod.AnalysisMethodID;
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void UpdateAnalysisMethod()
        {
            try
            {
                #region Old code
                //comm.CommandType = CommandType.StoredProcedure;
                //comm.CommandText = "UpdateAnalysisMethod";
                //comm.Parameters.Clear();

                //SqlParameter param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "Name";
                //param.Value = analysisMethodName;
                //comm.Parameters.Add(param);

                //param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "Group";
                //param.Value = analysisMethodGroupID;
                //comm.Parameters.Add(param);

                //param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "NameFR";
                //param.Value = analysisMethodNameFR;
                //comm.Parameters.Add(param);

                //param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "NameEN";
                //param.Value = analysisMethodNameEN;
                //comm.Parameters.Add(param);

                //param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "NameDE";
                //param.Value = analysisMethodNameDE;
                //comm.Parameters.Add(param);

                //param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "Version";
                //param.Value = analysisMethodVersion;
                //comm.Parameters.Add(param);

                //param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "Active";
                //param.Value = active;
                //comm.Parameters.Add(param);

                //param = new SqlParameter();
                //param.Direction = ParameterDirection.Input;
                //param.ParameterName = "ID";
                //param.Value = analysisMethodID;
                //comm.Parameters.Add(param);

                //db.ExecuteNonQuery(comm,trans);
                #endregion

                var analysisToBeUpdated = from a in Common.AnalyseMethod().AnalysisMethods
                                          where a.AnalysisMethodID == analysisMethodID
                                          select a;

                var item = analysisToBeUpdated.Single();

                item.Active = active;
                item.AnalysisMethodGroupID_FK = analysisMethodGroupID;
                item.AnalysisMethodName = analysisMethodName;
                item.AnalysisMethodNameDE = analysisMethodNameDE;
                item.AnalysisMethodNameEN = analysisMethodNameEN;
                item.AnalysisMethodNameFR = analysisMethodNameFR;
                item.AnalysisMethodScientificVersion = analysisMethodVersion;
                Common.AnalyseMethod().SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }  
        }
        #endregion
    }
}
